<template>
	<div>
		<bfe-form :model="formObj" ref="formObj" label-position="top">
			<div class="title">现状</div>
			<vxe-table :data="formObj.prjTsqList" border class="tableList">
				<vxe-table-column title="项目">
					<template #default="{ row, rowIndex }">
						<div>{{ formObj.prjTsqList[rowIndex].prj }}</div>
					</template>
				</vxe-table-column>
				<vxe-table-column title="现状">
					<template #default="{ row, rowIndex }">
						<bfe-form-item label :prop="`prjTsqList.${rowIndex}.tsq`">
							<bfe-input v-if="type !== 'view'" v-model="formObj.prjTsqList[rowIndex].tsq" size="small"
								:maxlength="1000" type="input" :placeholder="placeholderList[rowIndex]"></bfe-input>
							<div v-else>{{ formObj.prjTsqList[rowIndex].tsq }}</div>
						</bfe-form-item>
					</template>
				</vxe-table-column>
			</vxe-table>
			<div class="title">模型清单</div>
			<vxe-table :data="formObj.modlListList" border class="tableList">
				<vxe-table-column title="模型编号">
					<template #default="{ row, rowIndex }">
						<bfe-form-item label :prop="`modlListList.${rowIndex}.modlRefno`">
							<div>{{ row.modlRefno }}</div>
						</bfe-form-item>
					</template>
				</vxe-table-column>
				<vxe-table-column title="模型名称">
					<template #default="{ row, rowIndex }">
						<bfe-form-item label :prop="`modlListList.${rowIndex}.modlName`">
							<div>{{ row.modlName }}</div>
						</bfe-form-item>
					</template>
				</vxe-table-column>
				<vxe-table-column title="监控目的/逻辑描述">
					<template #default="{ row, rowIndex }">
						<bfe-form-item :prop="`modlListList.${rowIndex}.modlDes`">
							<div>{{ row.modlDes }}</div>
						</bfe-form-item>
					</template>
				</vxe-table-column>
				<vxe-table-column title="运行状态">
					<template #default="{ row, rowIndex }">
						<bfe-form-item :prop="`modlListList.${rowIndex}.oprtSts`">
							<div>
								{{ getLabelByValue(row.oprtSts) }}
							</div>
						</bfe-form-item>
					</template>
				</vxe-table-column>
				<vxe-table-column title="操作" width="180" v-if="type !== 'view'">
					<template #default="{ row, rowIndex }">
						<bfe-button type="text" @click="edit(row, rowIndex)">编辑</bfe-button>
						<bfe-button type="text" @click="delMolList(row, rowIndex)" v-if="modlListListClone && modlListListClone.length > 1">删除</bfe-button>
						<bfe-button type="text" @click="addMolList"
							v-if="rowIndex === formObj.modlListList.length - 1">新增</bfe-button>
					</template>
				</vxe-table-column>
			</vxe-table>
			<div>
				<vxe-pager border :current-page="modlTablePage.pageNo" :page-size="modlTablePage.pageSize" :page-sizes="[5,10,20,40]"
					:total="modlTablePage.total" :layouts="[
						'PrevPage',
						'JumpNumber',
						'NextPage',
						'FullJump',
						'Sizes',
						'Total',
					]" @page-change="handleModlPageChange">
				</vxe-pager>
			</div>
			<div class="title">反洗钱模型运行情况统计表</div>
			<vxe-table :data="formObj.modlOprtInfoStatList" ref="modlOprtInfoRef" :show-footer="true"
				:footer-method="footerMethod" border class="tableList">
				<vxe-table-column title="序号">
					<template #default="{ row, rowIndex }">
						{{ rowIndex + 1 + computeSn }}
					</template>
				</vxe-table-column>
				<vxe-table-column title="模型编号">
					<template #default="{ row, rowIndex }">
						<bfe-form-item label :prop="`modlOprtInfoStatList.${rowIndex}.modlRefno`">
							<div>{{ row.modlRefno }}</div>
						</bfe-form-item>
					</template>
				</vxe-table-column>
				<vxe-table-column title="模型名称">
					<template #default="{ row, rowIndex }">
						<bfe-form-item label :prop="`modlOprtInfoStatList.${rowIndex}.modlName`">
							<div>{{ row.modlName }}</div>
						</bfe-form-item>
					</template>
				</vxe-table-column>
				<vxe-table-colgroup :title="date">
					<vxe-table-column title="预警量" field="warnAmot">
						<template #default="{ row, rowIndex }">
							<bfe-form-item label :prop="`modlOprtInfoStatList.${rowIndex}.warnAmot`"
								:rules="formRules.txnDnum">
								<bfe-input v-if="type !== 'view'"
									v-model="row.warnAmot"
									@input="$refs.modlOprtInfoRef.updateFooter()" :maxlength="12"
									:placeholder="$t('stm.common.pleaseInput')"></bfe-input>
								<div v-else>
									{{ row.warnAmot }}
								</div>
							</bfe-form-item>
						</template>
					</vxe-table-column>
					<vxe-table-column title="完成甄别数量" field="complChkQanty">
						<template #default="{ row, rowIndex }">
							<bfe-form-item label :prop="`modlOprtInfoStatList.${rowIndex}.complChkQanty`"
								:rules="formRules.txnDnum">
								<bfe-input v-if="type !== 'view'"
									v-model="row.complChkQanty"
									@input="$refs.modlOprtInfoRef.updateFooter()" :maxlength="12"
									:placeholder="$t('stm.common.pleaseInput')"></bfe-input>
								<div v-else>
									{{ row.complChkQanty }}
								</div>
							</bfe-form-item>
						</template>
					</vxe-table-column>
					<vxe-table-column title="确认可疑数量" field="cfmSspcsQanty">
						<template #default="{ row, rowIndex }">
							<bfe-form-item label :prop="`modlOprtInfoStatList.${rowIndex}.cfmSspcsQanty`"
								:rules="formRules.txnDnum">
								<bfe-input v-if="type !== 'view'"
									v-model="row.cfmSspcsQanty"
									@input="$refs.modlOprtInfoRef.updateFooter()" :maxlength="12"
									:placeholder="$t('stm.common.pleaseInput')"></bfe-input>
								<div v-else>
									{{ row.cfmSspcsQanty }}
								</div>
							</bfe-form-item>
						</template>
					</vxe-table-column>
				</vxe-table-colgroup>
			</vxe-table>
			<div>
				<vxe-pager border :current-page="oprTablePage.pageNo" :page-size="oprTablePage.pageSize" :page-sizes="[5,10,20,40]"
					:total="oprTablePage.total" :layouts="[
						'PrevPage',
						'JumpNumber',
						'NextPage',
						'FullJump',
						'Sizes',
						'Total',
					]" @page-change="handleOprPageChange">
				</vxe-pager>
			</div>
		</bfe-form>

		<bfe-dialog :title="modelEdit ? $t('stm.common.edit') : $t('stm.common.add')" :close-on-click-modal="false"
			:visible.sync="show" @close="handCancel" width="50%">
			<bfe-form ref="editForm" :model="editForm" label-position="top">
				<bfe-form-item label="模型编号" prop="modlRefno" required>
					<bfe-input v-model="editForm.modlRefno" :disabled="!!editObj.modlName" size="small" type="input"
						:maxlength="50" :placeholder="$t('stm.common.pleaseInput')"></bfe-input>
				</bfe-form-item>

				<bfe-form-item label="模型名称" prop="modlName" required>
					<bfe-input v-model="editForm.modlName" :disabled="!!editObj.modlName" size="small" type="input"
						:maxlength="200" :placeholder="$t('stm.common.pleaseInput')"></bfe-input>
				</bfe-form-item>

				<bfe-form-item label="监控目的/逻辑描述" prop="modlDes">
					<bfe-input v-model="editForm.modlDes" size="small" type="input" :maxlength="1000"
						:placeholder="$t('stm.common.pleaseInput')"></bfe-input>
				</bfe-form-item>

				<bfe-form-item label="运行状态" prop="oprtSts">
					<bfe-select v-model="editForm.oprtSts" size="small" :placeholder="$t('tooltip.please-select')">
						<bfe-option v-for="item in selectObj['STM_modlOprtSts'][language]" :key="item.value"
							:label="item.label" :value="item.value">
						</bfe-option>
					</bfe-select>
				</bfe-form-item>
			</bfe-form>

			<span slot="footer" class="dialog-footer">
				<bfe-button @click="handCancel">{{
					$t("stm.common.cancel")
				}}</bfe-button>

				<bfe-button type="primary" @click="handConfirm">{{
					$t("stm.common.confirm")
				}}</bfe-button>
			</span>
		</bfe-dialog>
	</div>
</template>

<script>
import { mapState } from "vuex";
import moment from "moment";

export default {
	props: {
		type: {
			type: String,
			default: "",
		},
		date: {
			type: String,
			default: "",
		},
		selectObj: {
			type: Object,
			default: () => { },
		},
    reqOrgNo: {
      type: String,
      default: null,
    }
	},
	data(props) {
		return {
			modlListListClone:[],
			modlOprtInfoStatListClone:[],
			formObj: {
				prjTsqList: [
					{ prj: "人员", tsq: "" },
					{ prj: "制度", tsq: "" },
					{ prj: "系统", tsq: "" }
				],
				modlListList: [],
				modlOprtInfoStatList: [],
			},
			globalId: 1, //全局id，唯一标识
			oprGlobalId: 1, //全局id，唯一标识
			show: false,
			editIndex: null,
			editObj: {},
			editForm: {},
			modelEdit: false,
			// 分页
			modlTablePage: {
				pageNo: 1,
				pageSize: 5,
				total: 0,
			},
			// 分页
			oprTablePage: {
				pageNo: 1,
				pageSize: 5,
				total: 0,
			},
			formRules: {
				txnDnum: [{
					validator: (rule, value, callback) => {
						if (value === '' || value === null || value === undefined) {
                            // 如果字段非必填，空值通过；如果是必填，请在 rules 中加 required: true
                            return callback();
                        }
                      const str = String(value).trim();
                        const regex = /^(?:0|[1-9]\d*)$/;

                        if (regex.test(str)) {
                            callback();
                        } else {
                            callback(new Error(this.$t('stm.branchTransReport.pleaseInputInt')))
                        }
					},
					required: false,
					trigger: 'blur'
				}],
			},
			placeholderList: ['填写报告机构负责反洗钱可疑交易监控、甄别和报告的人员情况，主要人员具体姓名职位等',
				'填写报告机构反洗钱可疑交易监控、甄别和报告的现有制度和修订情况',
				'填写报告机构用于可疑交易监控的系统名称和供应商名称']
		};
	},
	computed: {
		...mapState({
			language: (state) => state.base.language,
			userInfo: (state) => state.base.userInfo,
		}),
		//模型列表当前页数据
		/*modlCurrentPageData(){
			const start = (this.modlTablePage.pageNo - 1) *this.modlTablePage.pageSize;
			const end = start + this.modlTablePage.pageSize
			return this.formObj.modlListList.slice(start,end)
		},*/
		//模型统计清单列表当前页数据
		/*modlOprCurrentPageData(){
			const start = (this.oprTablePage.pageNo - 1) *this.oprTablePage.pageSize;
			const end = start + this.oprTablePage.pageSize
			return this.formObj.modlOprtInfoStatList.slice(start,end)
		},*/
		//计算序号，翻页序号保持递增
		computeSn(index) {
			return (this.oprTablePage.pageNo - 1) * this.oprTablePage.pageSize;
		}
	},
	created() {
		this.initData()
	},
	methods: {
		//模型列表当前页数据
		modlCurrentPageData(){
			const start = (this.modlTablePage.pageNo - 1) *this.modlTablePage.pageSize;
			const end = start + this.modlTablePage.pageSize
			this.formObj.modlListList=this.modlListListClone.slice(start,end)
		},
		modlOprCurrentPageData(){
			const start = (this.oprTablePage.pageNo - 1) *this.oprTablePage.pageSize;
			const end = start + this.oprTablePage.pageSize
			this.formObj.modlOprtInfoStatList = this.modlOprtInfoStatListClone.slice(start,end)
		},
		// 初始化数据
		initData() {
			if (this.type !== "add") {
				const params = {
					optCode: "STM002GET1802",
					payload: {
						rptOrgNo: this.reqOrgNo,
						rptpd: this.date,
					},
					pageInfo: {
						pageNo: 1,
						pageSize: 1000, //查所有
					},
				};
				// 现状查询
				this.$p2svr(this.$api["_01_TSTM02MONRPT1800"], "RequestPoAuth", {
					...params,
				}).then((res) => {
					//console.log(res.payload, "非add现状详情打印");
					if (!res.payload || !Array.isArray(res.payload) || res.payload.length === 0) {
						if (this.type !== "view") {
							this.formObj.prjTsqList = [
								{ prj: "人员", tsq: "" },
								{ prj: "制度", tsq: "" },
								{ prj: "系统", tsq: "" }
							];
						}
					} else {
						this.formObj.prjTsqList = res.payload;
					}
				});
				// 模型清单查询
				this.$p2svr(this.$api["_01_TSTM02MONRPT1800"], "RequestPoAuth", {
					...params,
					optCode: "STM002GET1803",
				}).then((res) => {
					//console.log(res, "非add模型清单详情打印res");

					if (!res.payload || !Array.isArray(res.payload) || res.payload.length === 0) {
						if (this.type !== "view") {
							this.modlListListClone = [
								{ modlRefno: "", modlName: "", modlDes: "", oprtSts: "" ,id:1},
							];
							this.modlTablePage.total = 1
							this.globalId = 1
						}
					} else {
						/*this.formObj.modlListList = res.payload;
						this.modlTablePage.total = res.pageInfo ? res.pageInfo.total : 0
						this.modlTablePage.pageNo = res.pageInfo ? res.pageInfo.pageNo : 1
						this.modlTablePage.pageSize = res.pageInfo ? res.pageInfo.pageSize : 10*/

						//前端分页，接口返回所有数据-模型清单
						this.modlListListClone = res.payload.map((item,index) => ({ ...item, id:index+1 }));
						this.globalId = this.modlListListClone.length
						this.modlTablePage.total = this.modlListListClone.length
						this.modlCurrentPageData()
						this.modlOprCurrentPageData()
						/*console.log("非add模型清单this.modlListListClone")
						console.log(this.modlListListClone)
						console.log("非add模型运行统计modlOprtInfoStatListClone")
						console.log(this.modlOprtInfoStatListClone)*/
					}
				});
				// 模型运行情况
				this.$p2svr(this.$api["_01_TSTM02MONRPT1800"], "RequestPoAuth", {
					...params,
					optCode: "STM002GET1804",
				}).then((res) => {
					//console.log(res.payload, "编辑或查看-模型统计打印");
					/*this.formObj.modlOprtInfoStatList =
						res.payload.modlOprtInfoStatList || [];
					this.oprTablePage.total = res.pageInfo ? res.pageInfo.total : 0
					this.oprTablePage.pageNo = res.pageInfo ? res.pageInfo.pageNo : 1
					this.oprTablePage.pageSize = res.pageInfo ? res.pageInfo.pageSize === 0 ? 5 : res.pageInfo.pageSize : 5*/

					//前端分页，接口返回所有数据-模型运行统计
					this.modlOprtInfoStatListClone = res.payload.modlOprtInfoStatList.map((item,index) => ({ ...item, id:index+1 }));
					this.oprGlobalId = this.modlOprtInfoStatListClone.length
					this.oprTablePage.total = this.modlOprtInfoStatListClone.length
					this.modlCurrentPageData()
					this.modlOprCurrentPageData()
					/*console.log("非add模型运行清单this.modlListListClone")
					console.log(this.modlListListClone)
					console.log("非add模型运行清单modlOprtInfoStatListClone")
					console.log(this.modlOprtInfoStatListClone)*/
				});
			} else { //新增
				const params = {
					optCode: "STM002GET1802",
					payload: {
						rptOrgNo: this.userInfo.userAhrOrg,
						rptpd: this.date,
						queryFrom: 'add'
					},
					pageInfo: {
						pageNo: 1,
						pageSize: 1000, //查所有
					},
				};
				// 现状查询
				this.$p2svr(this.$api["_01_TSTM02MONRPT1800"], "RequestPoAuth", {
					...params,
				}).then((res) => {
					if (!res.payload || !Array.isArray(res.payload) || res.payload.length === 0) {
						this.formObj.prjTsqList = [
							{ prj: "人员", tsq: "" },
							{ prj: "制度", tsq: "" },
							{ prj: "系统", tsq: "" }];
					} else {
						this.formObj.prjTsqList = res.payload;
					}
				});
				// 模型清单查询
				this.$p2svr(this.$api["_01_TSTM02MONRPT1800"], "RequestPoAuth", {
					...params,
					optCode: "STM002GET1803",
				}).then((res) => {
					//console.log(res,"init-新增-初始模型清单res")
					if (!res.payload || !Array.isArray(res.payload) || res.payload.length === 0) {
						this.modlListListClone = [
							{ modlRefno: "", modlName: "", modlDes: "", oprtSts: "",id: this.globalId },
						];
						this.modlTablePage.total = 1
						//console.log(this.modlListListClone,"init-新增if-初始模型清单this.modlListListClone")
						this.modlCurrentPageData()
						this.modlOprCurrentPageData()
					} else {
						//console.log(res,"else新增-初始模型清单list")
						/*this.formObj.modlListList = res.payload;
						this.modlTablePage.total = res.pageInfo ? res.pageInfo.total : 0
						this.modlTablePage.pageNo = res.pageInfo ? res.pageInfo.pageNo : 1
						this.modlTablePage.pageSize = res.pageInfo ? res.pageInfo.pageSize : 10*/

						//add 前端分页，接口返回所有数据-模型清单
						this.modlListListClone = res.payload.map((item,index) => ({ ...item, id:index+1 }));
						this.globalId = this.modlListListClone.length
						this.modlTablePage.total = this.modlListListClone.length
						//console.log(this.modlListListClone,"init-新增else-初始模型清单this.modlListListClone")
						//初始新增若是当期有上期的模型清单，当期模型运行情况也需要同步
						const filteredData = this.modlListListClone.filter(item => item.oprtSts === '2');
                        // 将筛选后的数据映射到目标结构
						const mappedData = filteredData.map((item, index) => ({
							modlRefno: item.modlRefno,
							modlName: item.modlName,
							warnAmot: "",
							complChkQanty: "",
							cfmSspcsQanty: "",
							id: index+1
						}));
						// 将映射后的数据赋值给 modlOprtInfoStatListClone
						this.modlOprtInfoStatListClone = mappedData;
						this.oprGlobalId = this.modlOprtInfoStatListClone.length
						this.oprTablePage.total = this.modlOprtInfoStatListClone.length
						//console.log(this.modlOprtInfoStatListClone,"init-新增else-初始模型运行清单this.modlOprtInfoStatListClone")

						this.modlCurrentPageData()
						this.modlOprCurrentPageData()
					}
				});
				// 模型运行情况
				/*this.$p2svr(this.$api["_01_TSTM02MONRPT1800"], "RequestPoAuth", {
					...params,
					optCode: "STM002GET1804",
				}).then((res) => {
					console.log(res, "init-新增-模型统计打印res");
					/!*this.formObj.modlOprtInfoStatList =
						res.payload.modlOprtInfoStatList || [];
					this.oprTablePage.total = res.pageInfo ? res.pageInfo.total : 0
					this.oprTablePage.pageNo = res.pageInfo ? res.pageInfo.pageNo : 1
					this.oprTablePage.pageSize = res.pageInfo ? res.pageInfo.pageSize === 0 ? 5 : res.pageInfo.pageSize : 5*!/
					//add 前端分页，接口返回所有数据-模型运行统计
					if(res.payload && Array.isArray(res.payload) && res.payload.modlOprtInfoStatList.length > 0) {
						console.log(this.oprGlobalId,"init-新增-初始模型清单this.oprGlobalId1")
						this.modlOprtInfoStatListClone = res.payload.modlOprtInfoStatList.map((item,index) => ({ ...item, id:index+1 }));
						this.oprGlobalId = this.modlOprtInfoStatListClone.length
						console.log(this.oprGlobalId,"init-新增-初始模型清单this.oprGlobalId2")
						this.oprTablePage.total = this.modlOprtInfoStatListClone.length
						console.log(this.modlOprtInfoStatListClone,"init-新增-初始模型清单this.modlOprtInfoStatListClone")
						this.modlCurrentPageData()
						this.modlOprCurrentPageData()
					}

				});*/
			}

			//
			//console.log("初始模型清单this.modlListListClone")
			//console.log(this.modlListListClone)
			//console.log("初始模型运行统计modlOprtInfoStatListClone")
			//console.log(this.modlOprtInfoStatListClone)
			//console.log(this.modlTablePage)

			//console.log(this.oprTablePage)
			this.modlCurrentPageData()
			this.modlOprCurrentPageData()
		},
		// 分页
		handleModlPageChange({ currentPage, pageSize }) {
			this.modlTablePage.pageNo = currentPage;
			this.modlTablePage.pageSize = pageSize;

			//前端分页
			//console.log("模型清单点击翻页后this.modlListListClone")
			//console.log(this.modlListListClone)
			this.modlCurrentPageData()
			this.modlOprCurrentPageData()
			/*const params = {
				optCode: "STM002GET1803",
				payload: {
					rptOrgNo: this.userInfo.userAhrOrg,
					rptpd: this.date,
				},
				pageInfo: {
					pageNo: currentPage,
					pageSize: pageSize,
				},
			};
			this.$p2svr(this.$api["_01_TSTM02MONRPT1800"], "RequestPoAuth", {
				...params,
			}).then((res) => {
				this.formObj.modlListList = res.payload || [];
				this.modlTablePage.total = res.pageInfo ? res.pageInfo.total : 0
				this.modlTablePage.pageNo = res.pageInfo ? res.pageInfo.pageNo : 1
				this.modlTablePage.pageSize = res.pageInfo ? res.pageInfo.pageSize : 10
			});*/
		},
		// 分页
		handleOprPageChange({ currentPage, pageSize }) {
			this.oprTablePage.pageNo = currentPage;
			this.oprTablePage.pageSize = pageSize;
			//前端分页
			//console.log("模型运行统计点击翻页后this.modlOprtInfoStatListClone")
			//console.log(this.modlOprtInfoStatListClone)

			this.modlCurrentPageData()
			this.modlOprCurrentPageData()

			/*const params = {
				optCode: "STM002GET1804",
				payload: {
					rptOrgNo: this.userInfo.userAhrOrg,
					rptpd: this.date,
				},
				pageInfo: {
					pageNo: currentPage,
					pageSize: pageSize,
				},
			};
			this.$p2svr(this.$api["_01_TSTM02MONRPT1800"], "RequestPoAuth", {
				...params,
			}).then((res) => {
				this.formObj.modlOprtInfoStatList = res.payload.modlOprtInfoStatList || [];
				this.oprTablePage.total = res.pageInfo ? res.pageInfo.total : 0
				this.oprTablePage.pageNo = res.pageInfo ? res.pageInfo.pageNo : 1
				this.oprTablePage.pageSize = res.pageInfo ? res.pageInfo.pageSize : 10
			});*/
		},
		// 模型删除
		delMolList(row, rowindex) {
			const index =  this.modlListListClone.findIndex(item => item.id === row.id)
			//console.log("删除index")
			//console.log(index)

			//需要删除全局序号row.id对应的数据
			this.modlListListClone = this.modlListListClone.filter(item => item.id !== row.id)
			this.modlCurrentPageData()
			//console.log(this.modlListListClone,"删除后-this.modlListListClone")
			//console.log(this.formObj.modlListList,"删除后-this.formObj.modlListList")
			//删除后当前页为空，且不是第一页，跳转到前一页
			if(this.formObj.modlListList.length === 0 && this.modlTablePage.pageNo > 1){
				this.modlTablePage.pageNo--
			}
			this.modlTablePage.total = this.modlListListClone.length
			this.modlCurrentPageData()
			//console.log(this.modlListListClone,"删除跳转前一页后-this.modlListListClone")
			//console.log(this.formObj.modlListList,"删除跳转前一页后-this.formObj.modlListList")

			/*this.formObj.modlListList.splice(index, 1);
			if (this.modlTablePage.total > 0) {
				this.modlTablePage.total = this.modlTablePage.total - 1
			}*/
			// 如果是删除最后一条 则添加一条空数据
			if (this.modlListListClone && this.modlListListClone.length === 0) {
				this.modlListListClone = [{ modlRefno: "", modlName: "", modlDes: "", oprtSts: "",id:1 }]
				this.modlTablePage.total = 1
			}
			this.modlCurrentPageData()
			this.modlOprCurrentPageData()

			// 启用状态删除 下面才会删除
			if (row.oprtSts === "2") {
				// 查询 下面列表的下表并删除
				const statIndex = this.modlOprtInfoStatListClone.findIndex(
					item => item.modlRefno === row.modlRefno
				);
				if (statIndex !== -1) {
					this.modlOprtInfoStatListClone.splice(statIndex, 1);
					/*if (this.oprTablePage.total > 0) {
						this.oprTablePage.total = this.oprTablePage.total - 1
					}*/
				}
			}
			this.modlOprCurrentPageData()
			//console.log("删除-启用状态判断后this.modlOprtInfoStatListClone")
			//console.log(this.modlOprtInfoStatListClone)
			//console.log(this.formObj.modlOprtInfoStatList,"删除-启用状态判断后this.formObj.modlOprtInfoStatList")

			//删除后当前页为空，且不是第一页，跳转到前一页
			if(this.formObj.modlOprtInfoStatList.length === 0 && this.oprTablePage.pageNo > 1){
				this.oprTablePage.pageNo--
			}
			this.oprTablePage.total = this.modlOprtInfoStatListClone.length

			this.modlCurrentPageData()
			this.modlOprCurrentPageData()

		},
		// 模型新增
		addMolList() {
			//this.globalId++
			this.modelEdit = false;
			this.editIndex = -1;
			this.editObj = {};
			this.editForm = { modlRefno: "", modlName: "", modlDes: "", oprtSts: "" };
			this.show = true;

			/*//如果新增后当前页为空，跳转到最后一页
			if(this.modlCurrentPageData.length === 0){
				this.modlTablePage.pageNo = Math.ceil(this.formObj.modlListList.length/this.modlTablePage.pageSize)
			}
			this.modlTablePage.total = this.formObj.modlListList.length

			//同步模型运行情况
			// 从模型列表中筛选出启用状态的数据
			const enabledModels = this.formObj.modlListList.filter(model => model.oprtSts !== "2");

			console.log("模型清单delList-row后this.formObj.enabledModels")
			console.log(enabledModels)
			this.formObj.modlOprtInfoStatList = enabledModels
			//如果新增后当前页为空，跳转到最后一页
			if(this.modlOprCurrentPageData.length === 0){
				this.oprTablePage.pageNo = Math.ceil(this.formObj.modlOprtInfoStatList.length/this.oprTablePage.pageSize)
			}
			this.oprTablePage.total = this.formObj.modlOprtInfoStatList.length*/

		},
		// 模型编辑
		edit(row, index) {
			//console.log(row, index, "打印编辑");
			this.modelEdit = true;
			//this.editIndex = index;
			this.editIndex = row.id; //全局id
			this.editObj = { ...row };
			this.editForm = { ...row };
			this.show = true;
			//console.log(this.editObj,"this.editObj")
		},
		// 取消编辑
		handCancel() {
			this.show = false;
			this.editForm = {};
			this.editObj = {};
			this.editIndex = null;
		},
		// 确认编辑
		handConfirm() {
			this.$refs.editForm.validate((valid) => {
				if (valid) {
					console.log(this.modelEdit, this.formObj.modlOprtInfoStatList, this.editObj, this.editForm, "验证成功");
					// 编辑
					if (this.modelEdit) {
						//console.log(this.modelEdit,"this.modelEdit=true")
						if (this.editIndex !== null) {
							//console.log(this.editIndex,"this.editIndex!== null")
							const statIndex = this.modlListListClone.findIndex(
									item => item.id === this.editIndex
							);
							//console.log(statIndex,"statIndex")
							this.$set(this.modlListListClone, statIndex, {
								...this.editForm,
							});
							//console.log(this.modlListListClone,"this.editIndex!== null-this.modlListListClone")
							//console.log(this.editObj.modlRefno,"this.editObj.modlRefno")
							this.modlCurrentPageData()
							this.modlOprCurrentPageData()
							if (!this.editObj.modlRefno && this.editForm.oprtSts === "2") {
								//  如果是已有名称的模型，不更新下方表格
								this.modlOprtInfoStatListClone.push({
									modlRefno: this.editForm.modlRefno,
									modlName: this.editForm.modlName,
									warnAmot: "",
									complChkQanty: "",
									cfmSspcsQanty: "",
									id:this.oprGlobalId++
								});
								//this.oprTablePage.total = this.oprTablePage.total + 1
								this.oprTablePage.total = this.modlOprtInfoStatListClone.length
								this.show = false;

								//console.log(this.modlListListClone,"没有更新this.editIndex!== null-this.modlListListClone")
								//console.log(this.modlOprtInfoStatListClone,"没有更新this.editIndex!== null-this.modlOprtInfoStatListClone")
								this.modlCurrentPageData()
								this.modlOprCurrentPageData()
								return
							}
							// 如果修改了状态判断是否需要显示
							if (
								this.editObj.oprtSts === "2" &&
								this.editForm.oprtSts !== "2"
							) {
								//console.log("修改了状态删除")
								// 查询 下面列表的下表并删除
								const statIndex = this.modlOprtInfoStatListClone.findIndex(
									item => item.modlRefno === this.editForm.modlRefno
								);
								if (statIndex !== -1) {
									this.modlOprtInfoStatListClone.splice(statIndex, 1);
									this.oprTablePage.total = this.modlOprtInfoStatListClone.length
									this.modlCurrentPageData()
									this.modlOprCurrentPageData()
									/*if (this.oprTablePage.total > 0) {
										this.oprTablePage.total = this.oprTablePage.total - 1
									}*/
								}
								//console.log(this.modlListListClone,"this.editIndex!== null-修改了状态判断删除this.modlListListClone")
								//console.log(this.modlOprtInfoStatListClone,"this.editIndex!== null-修改了状态判断删除this.modlOprtInfoStatListClone")
								this.show = false;
								return
							}
							if (
								this.editObj && this.editObj.oprtSts !== "2" &&
								this.editForm.oprtSts === "2"
							) {
								//console.log(this.oprGlobalId,"编辑时候，如果之前是禁用，现在启用，则添加到下面this.oprGlobalId")
								// 编辑时候，如果之前是禁用，现在启用，则添加到下面
								this.modlOprtInfoStatListClone.push({
									modlRefno: this.editForm.modlRefno,
									modlName: this.editForm.modlName,
									warnAmot: "",
									complChkQanty: "",
									cfmSspcsQanty: "",
									id: ++this.oprGlobalId
								});
								//this.oprTablePage.total = this.oprTablePage.total + 1
								this.oprTablePage.total = this.modlOprtInfoStatListClone.length
								this.modlCurrentPageData()
								this.modlOprCurrentPageData()
								//console.log(this.modlListListClone,"this.editIndex!== null-如果之前是禁用，现在启用this.modlListListClone")
								//console.log(this.modlOprtInfoStatListClone,"this.editIndex!== null-如果之前是禁用，现在启用this.modlOprtInfoStatListClone")

							}
							//console.log(this.oprTablePage, "打印模型运行分页器数据");
						}
					} else {
						// 新增必填
						if (!this.editForm.modlName || !this.editForm.modlRefno) {
							this.$message.error("请输入模型编号或者模型名称");
							return
						}
						// 新增判断当前是否已有该模型
						const isExist = this.formObj.modlListList.some(
							(item) =>
								item.modlName === this.editForm.modlName ||
								item.modlRefno === this.editForm.modlRefno
						);
						if (isExist) {
							this.$message.error("模型编号模型名称已存在");
							return;
						}
						// 新增 如果只有一条数据则替换
						if (
							this.modlListListClone.length === 1 &&
							!this.modlListListClone[0].modlRefno
						) {
							this.$set(this.modlListListClone, 0, { ...this.editForm,id:1});
							this.modlCurrentPageData()
							this.modlOprCurrentPageData()
							this.globalId++
							//console.log(this.modlListListClone,"新增只有一条数据modlListListClone")
							//console.log(this.modlOprtInfoStatListClone,"新增只有一条数据modlOprtInfoStatListClone")
							//console.log(this.globalId,"新增只有一条数据globalId")
						} else {
							this.globalId++
							//console.log(this.globalId,"新增一条数据globalId")
							//console.log(this.editForm,"新增一条数据editForm")
							this.modlListListClone.push({ ...this.editForm,id:this.globalId });
							this.modlTablePage.total = this.modlTablePage.total + 1
							this.modlCurrentPageData()
							this.modlOprCurrentPageData()
							//console.log(this.modlListListClone,"新增一条数据modlListListClone")
							//console.log(this.modlOprtInfoStatListClone,"新增一条数据modlOprtInfoStatListClone")
						}
						// 选择启用才可以添加到下面
						if (this.editForm.oprtSts === "2") {
							this.oprGlobalId++
							//console.log(this.oprGlobalId,"选择启用才可以添加到下面this.oprGlobalId")
							//console.log(this.modlOprtInfoStatListClone.length,"选择启用才可以添加到下面this.modlOprtInfoStatListClone.length")
							this.modlOprtInfoStatListClone.push({
								modlRefno: this.editForm.modlRefno,
								modlName: this.editForm.modlName,
								warnAmot: "",
								complChkQanty: "",
								cfmSspcsQanty: "",
								id: this.oprGlobalId
							});
							//this.oprGlobalId = this.modlOprtInfoStatListClone.length
							this.oprTablePage.total = this.modlOprtInfoStatListClone.length
							this.modlCurrentPageData()
							this.modlOprCurrentPageData()
							//console.log(this.modlListListClone,"选择启用才可以添加到下面modlListListClone")
							//console.log(this.modlOprtInfoStatListClone,"选择启用才可以添加到下面modlOprtInfoStatListClone")
						}
					}
					this.show = false;
				} else {
					console.log("验证失败");
					return false;
				}
			});
		},
		getLabelByValue(value) {
			const options = this.selectObj["STM_modlOprtSts"][this.language];
			const found = options?.find((item) => item.value === value);
			return found ? found.label : "--";
		},

		sumNumNew(costForm, type) {
			let total = 0;
			for (let i = 0; i < costForm.length; i++) {
				total += Number(costForm[i][type]);
			}
			//console.log(costForm, total, "totaltotaltotaltotal");

			return total || "--";
		},
		footerMethod({ columns, data }) {
			const footerData = [
				columns.map((column, _columnIndex) => {
					if (_columnIndex === 0) {
						return "合计";
					}
					/*console.log(
						column,
						column.property,
						"column.propertycolumn.propertycolumn.property"
					);*/

					// 传入需要计算合计的列
					if (
						["warnAmot", "complChkQanty", "cfmSspcsQanty"].includes(
							column.property
						)
					) {
						return this.sumNumNew(this.modlOprtInfoStatListClone, column.property);
					}
					return "--";
				}),
			];
			//console.log(footerData, "footerDatafooterDatafooterData");
			return footerData;
		},
		validateForm() {
			let flag = false
			this.$refs.formObj.validate((ok) => {
				if (ok) {
					flag = true
				} else {
					console.log("validateForm失败")
				}
			})
			return flag
		},
	},
};
</script>


<style lang="less" scoped>
.title {
	font-size: 18px;
	color: rgba(0, 0, 0, 0.85);
	font-weight: 600;
	margin: 10px 0;
}

.tableList {
	/deep/.giop-form-item {
		margin-bottom: 0;
	}

	/deep/.cell {
		padding: 6px 10px;
	}

	/deep/.giop-form-item__error {
		position: relative;
		padding: 0 0 4px;
	}

	/deep/.giop-input--small .giop-input__inner {
		height: 30px !important;
	}

	/deep/.giop-form-item {
		margin-bottom: 0;
	}

	/deep/.giop-cascader {
		width: 100%;
	}

	/deep/.giop-cascader--small .giop-cascader__label {
		line-height: 16px;
	}
}
</style>